package com.example.chat_websocket.service.impl;

import com.alibaba.fastjson.JSON;
import com.example.chat_websocket.pojo.User;
import com.example.chat_websocket.service.LoginService;
import com.example.chat_websocket.service.UserService;
import com.example.chat_websocket.utils.JWTUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @version 1.0
 * @Author 张云飞
 * @Date 2021/10/29 0029 21:25
 * @注释
 */
@Service
public class LoginServiceImpl implements LoginService {

@Autowired
private UserService userService;


    //加密源
    public static final String slat="huliascnasf";

    @Override
    public String login(User user) {
        /*
        检查参数是否合法
        根据用户名密码查询是否存在
        不存在失败
        存在使用jwt生成Token 返回给前段
        token放入Redis中，redis token：user 信息 设置过期时间
         */
        String username = user.getUsername();
        String password = user.getPassword();
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            return "1";//为空
        }

        password = DigestUtils.md5Hex(password + slat);
        //System.out.println("password-----------"+password);
        User temp = userService.findUser(username, password);
        if (temp == null) {

            return "2";//无用户
        }
        String token = JWTUtils.createToken(user.getUsername());
        //redisTemplate.opsForValue().set("TOKEN_"+token, JSON.toJSONString(username),1, TimeUnit.DAYS);
        return token;
    }


    @Override
    public User checkToken(String token) {
            if (StringUtils.isEmpty(token)){
                System.out.println("222222222222222222222222221111111111111");
                return null;
            }
            Map<String, Object> stringObjectMap = JWTUtils.checkToken(token);
            if (stringObjectMap==null){
                System.out.println("33333333333333333333333333333333333331111111");
                return null;
            }
            //String userJson=redisTemplate.opsForValue().get("TOKEN_"+token);
            if (StringUtils.isEmpty(token)){
                System.out.println("44444444444444444444444444444444111111");
                return null;
            }
            System.out.println("555555555555555555555555555111111111");
            User user1 = JSON.parseObject(token, User.class);
            return user1;
    }

    @Override
    public void logout(String token) {

    }

    @Override
    public String register(User user) {
        String username=user.getUsername();
        String password=user.getPassword();
        if (com.baomidou.mybatisplus.core.toolkit.StringUtils.isBlank(username)|| com.baomidou.mybatisplus.core.toolkit.StringUtils.isBlank(password)){
            return "1";//为空
        }
        User temp=this.userService.findByUsername(username);
        if (temp!=null){
            return "2";//用户名已存在
        }
        temp=new User(username,DigestUtils.md5Hex(password+slat));
        this.userService.save(temp);

        //token
        String token=JWTUtils.createToken(user.getUsername());
        //redisTemplate.opsForValue().set("TOKEN_"+token,JSON.toJSONString(sysUser),1,TimeUnit.DAYS);
        return token;
    }
}
